package com.workpaper.app.plan.view;

import java.util.Calendar;
import java.util.Date;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import com.workpaper.app.cache.resource.ViewInfoCache;
import com.workpaper.app.consts.AppConsts;
import com.workpaper.app.consts.plan.DailyPlanViewConsts;
import com.workpaper.app.plan.business.DailyPlanBusiness;
import com.workpaper.app.plan.entity.DailyPlan;
import com.workpaper.app.utils.platform.SWTPlatformUtils;
/**
 * 日常计划编辑窗口
 * @author zyj
 *
 */
public class DailyPlanEditDialog extends DailyPlanAddDialog {
	
	/**
	 * 待编辑数据
	 */
	private DailyPlan dailyPlan;
	
	
	public DailyPlanEditDialog(Shell parentShell, DailyPlan dailyPlan) {
		super(parentShell);
		this.dailyPlan = dailyPlan;
	}

	
	/**
	 * 配置窗口标题、图标等信息
	 * @param newShell
	 */
	@Override
	protected void configureShell(Shell newShell) {
		// TODO Auto-generated method stub
		super.configureShell(newShell);
		newShell.setText(ViewInfoCache.get(DailyPlanViewConsts.DailyPlanEditViewTitle));	//标题
//		newShell.setImage(null);	//图标
	}


	
	@Override
	protected Point getInitialSize() {
		return new Point(400, 400);
	}

	/**
	 * 内容设置
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		dateSelector.setEnabled(false);
		if(dailyPlan != null) {
			setEditData();
		}
		return container;
	}


	private void setEditData() {
		// TODO Auto-generated method stub
		Date planDate = dailyPlan.getPlanDate();
		Calendar wdCalendar = Calendar.getInstance();
		wdCalendar.setTime(planDate);
		dateSelector.setYear(wdCalendar.get(Calendar.YEAR));
		dateSelector.setMonth(wdCalendar.get(Calendar.MONTH));
		dateSelector.setDay(wdCalendar.get(Calendar.DAY_OF_MONTH));
		
		contentText.setText(dailyPlan.getContent());
		executorText.setText(dailyPlan.getExecutor());
		remarkText.setText(dailyPlan.getRemark());
	}

	@Override
	protected void okPressed() {
		DailyPlan dailyPlan = extractPlanData();
		dailyPlan.setId(this.dailyPlan.getId());
		DailyPlanBusiness.getInstance().updateById(dailyPlan);
		SWTPlatformUtils.getViewByID(AppConsts.DAILY_PLAN_MANAGE_VIEW, DailyPlanManageView.class).refreshTableData();

		setReturnCode(OK);
		close();
	}

}
